﻿@using Lombiq.HelpfulExtensions.Extensions.Flows.Models
@using OrchardCore.ContentManagement
@using OrchardCore.ContentManagement.Display
@using OrchardCore.Flows.Models
@using OrchardCore.Mvc.Utilities
@model OrchardCore.Flows.ViewModels.FlowPartViewModel
@inject IContentItemDisplayManager ContentItemDisplayManager

<section class="flow">
    @foreach (var widget in Model.FlowPart.Widgets)
    {
        var widgetContent = await ContentItemDisplayManager.BuildDisplayAsync(
            widget, Model.BuildPartDisplayContext.Updater, Model.BuildPartDisplayContext.DisplayType, Model.BuildPartDisplayContext.GroupId);
        var flowMetadata = widget.As<FlowMetadata>();

        if (flowMetadata != null)
        {
            var additionalStyling = widget.As<AdditionalStylingPart>();

            bool removeGridExtensionClasses = false;
            if (additionalStyling != null)
            {

                if (!string.IsNullOrEmpty(additionalStyling.CustomClasses))
                {
                    widgetContent.Classes.Add(additionalStyling.CustomClasses?.Trim());
                }

                removeGridExtensionClasses = additionalStyling.RemoveGridExtensionClasses;
            }

            if (!removeGridExtensionClasses)
            {
                widgetContent.Classes.Add("widget-align-" + flowMetadata.Alignment.ToString().ToLowerInvariant());
                widgetContent.Classes.Add("widget-size-" + flowMetadata.Size);
            }

            widgetContent.Classes.Add("widget");
            widgetContent.Classes.Add("widget-" + widget.ContentItem.ContentType.HtmlClassify());
        }

        @await DisplayAsync(widgetContent)
    }
</section>


